package com.whfc.fse.queue;

import com.alibaba.fastjson.JSON;
import com.whfc.common.constant.QueueConst;
import com.whfc.fse.dao.FseCraneMapper;
import com.whfc.fse.dto.FseCraneDataDTO;
import com.whfc.fse.dto.FseWarnDTO;
import com.whfc.fse.entity.FseCrane;
import com.whfc.fse.enums.FseType;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.manager.FseWarnMgr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author qzexing
 * @version 1.0
 * @date 2022/5/6 16:07
 */
@Component
@RabbitListener(queuesToDeclare = {@Queue(name = QueueConst.FSE_CRANE_WARN_DATA)}, concurrency = "1-2",containerFactory = "baseFactory")
public class FseCraneWarnDataQueueListener {

    private static final Logger logger = LoggerFactory.getLogger(FseCraneWarnDataQueueListener.class);

    @Autowired
    private FseCraneMapper fseCraneMapper;

    @Autowired
    private FseWarnMgr fseWarnMgr;

    @RabbitHandler
    public void handle(String msg) {
        try {
            logger.info("消息消费者处理塔机报警数据,msg:{}", msg);
            FseCraneDataDTO fseCraneData = JSON.parseObject(msg, FseCraneDataDTO.class);
            String sn = fseCraneData.getSn();
            String platform = fseCraneData.getPlatform();
            FseCrane fseCrane = fseCraneMapper.selectByPlatformAndSn(platform, sn);
            if (fseCrane == null) {
                logger.warn("该塔机不存在,platform:{},sn:{}", platform, sn);
                return;
            }
            // 处理报警数据
            if (fseCraneData.getCraneStatus() != null|| StringUtils.hasText(fseCraneData.getAlarmCause())) {
                FseWarnRuleType fseWarnRuleType = FseWarnRuleType.parseByValue(fseCraneData.getCraneStatus()==null?-1:fseCraneData.getCraneStatus());
                String triggerValue = this.getTriggerValue(fseWarnRuleType, fseCraneData);
                FseWarnDTO fseWarnDTO = new FseWarnDTO();
                fseWarnDTO.setDeptId(fseCrane.getDeptId());
                fseWarnDTO.setCode(fseCrane.getCode());
                fseWarnDTO.setFseType(FseType.CRANE);
                fseWarnDTO.setFseWarnRuleType(fseWarnRuleType);
                fseWarnDTO.setTriggerTime(fseCraneData.getTime());
                fseWarnDTO.setTriggerObjectId(fseCrane.getId());
                fseWarnDTO.setTriggerValue(triggerValue);
                if (StringUtils.hasText(fseCraneData.getAlarmCause())) {
                    fseWarnDTO.setTriggerValue(fseCraneData.getAlarmCause()+"--"+fseWarnDTO.getTriggerValue());
                }
                fseWarnMgr.handleFseWarn(fseWarnDTO);
            }
        } catch (Exception e) {
            logger.warn("处理塔机报警数据失败.", e);
        }
    }

    /**
     * 获取触发值
     *
     * @param ruleType
     * @param fseCraneDataDTO
     * @return
     */
    private String getTriggerValue(FseWarnRuleType ruleType, FseCraneDataDTO fseCraneDataDTO) {
        String triggerValue = "";
        switch (ruleType) {
            case CRANE_WEIGHT:
                triggerValue = String.valueOf(fseCraneDataDTO.getWeight());
                break;
            case CRANE_FORCE_MOMENT:
                triggerValue = String.valueOf(fseCraneDataDTO.getMomentRatio());
                break;
            case CRANE_RANGE:
                triggerValue = String.valueOf(fseCraneDataDTO.getRange());
                break;
            case CRANE_HEIGHT_RANGE:
                triggerValue = String.valueOf(fseCraneDataDTO.getHeight());
                break;
            case CRANE_ROTARY_LIMIT:
                triggerValue = String.valueOf(fseCraneDataDTO.getTurnAngle());
                break;
            case CRANE_WIND_SPEED:
                triggerValue = String.valueOf(fseCraneDataDTO.getWindSpeed());
                break;
            case CRANE_TURN:
                triggerValue = String.valueOf(fseCraneDataDTO.getDipAngle());
                break;
            default:
                triggerValue = "";
                break;
        }
        return triggerValue;

    }
}
